package cn.flying.cloud.system.user.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.bo.UserInfo;
import cn.flying.cloud.base.common.constants.Constants;
import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.service.SysUserInfoService;

/**
 * 用户信息表 前端控制器
 *
 * @author: admin
 * @date: 2023年04月19日 16:27
 * @version: 1.0
 */
@RestController
@RequestMapping("/system/user")
public class SysUserInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysUserInfoService sysUserInfoService;

    /**
     * 分页查询用户信息表数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    public PageResponse<SysUserInfo> page(@RequestBody PageRequest<SysUserInfo> request) {
        return sysUserInfoService.getPageList(request);
    }

    /**
     * 列表查询用户信息表数据
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody SysUserInfo sysUserInfo) {
        List<SysUserInfo> list = sysUserInfoService.getList(sysUserInfo);
        return Rt.success(list);
    }

    /**
     * 获取所有的用户信息表数据
     *
     * @return
     */
    @GetMapping("getAllUserList")
    public Rt<?> getAllUserList() {
        List<SysUserInfo> list = sysUserInfoService.getAllUserList();
        return Rt.success(list);
    }

    /**
     * 查询用户信息表详情数据
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(SysUserInfo sysUserInfo) {
        return Rt.success(sysUserInfoService.getOneObj(sysUserInfo));
    }

    /**
     * 根据主键ID查询用户信息表详情数据
     *
     * @param id
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        SysUserInfo sysUserInfo = sysUserInfoService.getById(id);
        return Rt.success(sysUserInfo);
    }

    /**
     * 保存用户信息表数据
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody SysUserInfo sysUserInfo) {
        if (Objects.isNull(sysUserInfo.getId())) {
            // 新增
            sysUserInfoService.createObj(sysUserInfo);
        } else {
            // 更新
            sysUserInfoService.updateObj(sysUserInfo);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除用户信息表数据
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(SysUserInfo sysUserInfo) {
        sysUserInfoService.deleteObj(sysUserInfo);
        return Rt.success("删除成功！");
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        sysUserInfoService.deleteById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 重置用户密码
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("reset")
    public Rt<?> reset(SysUserInfo sysUserInfo) {
        sysUserInfoService.reset(sysUserInfo);
        return Rt.success("密码重置成功！");
    }

    /**
     * 查询用户认证信息
     *
     * @param username
     * @param type
     * @return
     */
    @RequestMapping("/authInfo")
    public UserInfo getUserAuthInfo(String username, String type) {
        return sysUserInfoService.getUserAuthInfo(username, type);
    }

    /**
     * 同步注册用户信息
     *
     * @param authInfo
     * @return
     */
    @RequestMapping("/register")
    public String register(@RequestBody UserInfo authInfo) {
        return sysUserInfoService.register(authInfo);
    }


    /**
     * 修改当前登录用户信息
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("updateProfile")
    public Rt<?> updateProfile(@RequestBody SysUserInfo sysUserInfo) {
        String userId = (String) Ft.ThreadLocal.get(Constants.USER_ID);
        SysUserInfo query = new SysUserInfo();
        query.setUserId(userId);
        SysUserInfo userInfo = sysUserInfoService.getOneObj(query);
        userInfo.setName(sysUserInfo.getName());
        userInfo.setMobile(sysUserInfo.getMobile());
        userInfo.setEmail(sysUserInfo.getEmail());
        userInfo.setGender(sysUserInfo.getGender());
        sysUserInfoService.updateObj(userInfo);
        return Rt.success("修改成功！");
    }

    /**
     * 修改用户密码
     *
     * @param params
     * @return
     */
    @PostMapping("updateUserPwd")
    public Rt<?> updateUserPwd(@RequestBody Map<String, String> params) {
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        sysUserInfoService.updateUserPwd(oldPassword, newPassword);
        return Rt.success("修改成功！");
    }
}
